<?php

/**
* RenderSmarty
* 基于smarty的渲染视图对象
*
* @category smarty
* @package mvc
* @filesource
* @author    liuran <ebow@21cn.com>
* @copyright   Copyright 2005-2009, Liuran
* @license    http://www.php.net/license/3_01.txt     PHP License 3.0
* @version  CVS:$Id$
*/
  
 
class RenderSmarty extends QView_Render_PHP
{
    protected $_tpl = null; 

    // 自定义的输出函数
    protected $_help_function = 'Helper_CustomSmartyApi';
    
    /**
     * 构造函数
     *
     * @param array $config
     */
    function __construct(array $config = null)
    { 

        $this->file_extname = $config['file_extname'];
        $this->view_dir = $config['view_dir'];
        
        if (is_null($this->_tpl)) {     
            require_once( Q::ini('Smarty_Path') . '/Smarty.class.php' );
            $this->_tpl = new Smarty();
        }

        if (is_object($this->_tpl)) {
            if (is_array($config))
            {
                foreach ($config as $key => $value)
                {
                    $this->_tpl->{$key} = $value;
                }
            }

            // 载入自定义函数实现类。
            $classname = Q::loadClass($this->_help_function);
             
            new $classname($this->_tpl);
        }
 
    }

 
    /**
     * 指定模板变量
     *
     * @param string|array $key
     * @param mixed $data
     *
     * @return QView_Render_PHP
     */
    function assign($key, $data = null)
    {    
        return  $this->_tpl->assign($key, $data);
    }
 

    /**
     * 渲染视图
     *
     * @param string $viewname
     * @param array $vars
     * @param array $config
     */
    function display($viewname = null, array $vars = null, array $config = null)
    {
        if (empty($viewname))
        {
            $viewname = $this->_viewname;
        }
 
        echo $this->fetch($viewname, $vars, $config);
    }

    /**
     * 执行
     */
    function execute()
    {
        $this->display($this->_viewname);
    }


    
    /**
     *    isCached
     *    判断是否已经生成缓存。
     **   @param  $cacheId 唯一识别的缓存编号。
     *    @return boolean
     *    @access public
     */
    function isCached($cacheId = null) {  
        
        $view_dir = $this->view_dir; 
        $extname  = $this->file_extname;
        $viewname = $this->_viewname;
        $filename = "{$view_dir}/{$viewname}.{$extname}";     
        return $this->_tpl->is_cached($filename, $cacheId);
    }

    /**
     * 渲染视图并返回渲染结果
     *
     * @param string $viewname
     * @param array $vars
     * @param array $config
     *
     * @return string
     */
    function fetch($viewname, array $vars = null, array $config = null)
    {
        $this->_before_render();
        $view_dir = isset($config['view_dir']) ? $config['view_dir'] : $this->view_dir;
 
        $extname = isset($config['file_extname']) ? $config['file_extname'] : $this->file_extname;
        $filename = "{$view_dir}/{$viewname}.{$extname}"; 

        $context = QContext::instance();
        $cache_id = $context['cache_id'];

        $output = $this->_tpl->fetch($filename, $cache_id);
        $this->_after_render($output);
        return $output;
    }

} 